package com.bosssoft.trainee.rbac.controller;

import com.bosssoft.trainee.rbac.entity.dto.RolesDTO;
import com.bosssoft.trainee.rbac.entity.dto.UserDTO;
import com.bosssoft.trainee.rbac.entity.vo.CommonResult;
import com.bosssoft.trainee.rbac.entity.vo.RoleVO;
import com.bosssoft.trainee.rbac.entity.vo.UsersVO;
import com.bosssoft.trainee.rbac.service.RolesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@ResponseBody
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RolesService rolesService;

    @PostMapping("/insert")
    public CommonResult<RoleVO> insertRole(@RequestBody RoleVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        int result = rolesService.insertRole(rolesDTO);
        if(result>0){
            return new CommonResult(200,"登陆成功",result);
        }else{
            return new CommonResult(400,"插入失败",null);
        }
    }

    @PostMapping("/delete")
    public CommonResult<RoleVO> deleteRole(@RequestBody RoleVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        int result = rolesService.deleteRole(rolesDTO);
        if(result>0){
            return new CommonResult(200,"删除成功",result);
        }else{
            return new CommonResult(400,"删除失败",null);
        }
    }

    @PostMapping("/updateName")
    public CommonResult<RoleVO> updateRolesName(@RequestBody RoleVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        int result = rolesService.updateRolesName(rolesDTO);
        if(result>0){
            return new CommonResult(200,"更新成功",result);
        }else{
            return new CommonResult(400,"更新失败",null);
        }
    }

    @GetMapping("/getAll")
    public CommonResult<RoleVO> getAllRoles(){
        log.info("enter getall method");
        List<RolesDTO> rolesDTOList = rolesService.getAllRoles();
        if(rolesDTOList != null ){
            List<RoleVO> roleVOList = new ArrayList<>();
            BeanUtils.copyProperties(rolesDTOList,roleVOList);
            log.info("after getall method sucess");
            return new CommonResult(200,"查询成功",roleVOList);
        }else{
            log.info("after getall method filed");
            return new CommonResult(400,"查询失败",null);
        }
    }

    @GetMapping("/getUserRoles")
    public CommonResult<RoleVO> getUserRoles(@RequestBody UsersVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        if(usersDTO != null ){
            List<RolesDTO> rolesDTOList = rolesService.getUserRoles(usersDTO.getUserId());

            return new CommonResult(200,"该用户的所有角色",rolesDTOList);
        }else{
            return new CommonResult(400,"查询失败",null);
        }
    }
}
